package com.dysoft.flesh;

import com.dysoft.bones.DataManager;

import java.util.List;

/**
 * @author Sean Micklethwaite
 *         Aug 16, 2010 7:45:00 PM
 */
public class CompositeRenderer implements Renderer {
	final Renderer head, tail;

	public CompositeRenderer(Renderer head, Renderer tail) {
		this.head = head;
		this.tail = tail;
	}

	public void render(RenderContext ctx) throws DataManager.DataObject.NotPresentException {
		head.render(ctx);
		tail.render(ctx);
	}

	@Override
	public String toString() {
		return tail instanceof Nil ? head.toString() :
				head.toString() + ", " + tail.toString();
	}

	static class Nil implements Renderer {
		public void render(RenderContext ctx) throws DataManager.DataObject.NotPresentException { }
	}

	public static Renderer create(List<Renderer> renderers) {
		if(renderers.isEmpty()) {
			return new Nil();
		} else {
			return new CompositeRenderer(
				renderers.get(0),
				create(renderers.subList(1, renderers.size()))
			);
		}
	}
}
